﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Actuals.Business;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;
using System.Drawing;
namespace Actuals
{
    public partial class ResourceProjection : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                IBusinessEmployee businessEmployee = new BusinessEmployee();
                IList<IEmployee> employees = businessEmployee.GetEmployees();
                cblEmployees.DataSource = employees;
                cblEmployees.DataValueField = "Id";
                cblEmployees.DataTextField = "NickName";
                cblEmployees.DataBind();
               

            }
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            CreateDynamicTable();
        }

        public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
        {
            for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
                yield return day;
        }
        private void CreateDynamicTable()
        {
            PlaceHolder1.Controls.Clear();

            DateTime fromDate;
            string[] _date = txtFromDate.Text.Split('-');

            fromDate = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));

            DateTime toDate;
            string[] _date1 = txtToDate.Text.Split('-');

            toDate = new DateTime(int.Parse(_date1[2]), int.Parse(_date1[0]), int.Parse(_date1[1]));
            var o = new { Id = 1, Name = "" };
           // int selCount = 0;
            List<IEmployee> list = new List<IEmployee>();
            string listOfemp = string.Empty;
            for (int i = 0; i < cblEmployees.Items.Count; i++)
                if (cblEmployees.Items[i].Selected)
                {
                    IEmployee emp = new Employee();
                    emp.NickName = cblEmployees.Items[i].Text;
                    emp.Id = int.Parse(cblEmployees.Items[i].Value); ;
                    list.Add(emp);
                    listOfemp = listOfemp + emp.Id + ",";
                    //selCount++;
                }

            IBusinessResourceProjection businessResourceProjection = new BusinessResourceProjection
                ();
            IList<IResourceProjection> listofResourceProjections = businessResourceProjection.
                GetResourceProjection(listOfemp, fromDate, toDate);

            //int selectedCount = cblEmployees.Items.Cast<ListItem>().Count(li => li.Selected);

            // Fetch the number of Rows and Columns for the table 
            // using the properties
          //  int tblRows = selectedCount;
            //int tblCols = Columns;
            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Attributes.Add("Class", "gridtable");
            //tbl.CssClass = "table.gridtable";
            // Add the table to the placeholder control
            PlaceHolder1.Controls.Add(tbl);
            // Now iterate through the table and add your controls 
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Attributes.Add("Class", "th");
            tc.Text = "Name";
            tr.Cells.Add(tc);

            foreach (DateTime day in EachDay(fromDate, toDate))
            {
                TableCell tc1 = new TableCell();
                tc1.Text = day.ToString("MM/dd");

                //TextBox txtBox = new TextBox();
                //txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
                // Add the control to the TableCell
                //tc.Controls.Add(txtBox);
                // Add the TableCell to the TableRow
                tc1.Attributes.Add("Class", "gridtable");
                tr.Cells.Add(tc1);
            }
            // Add the TableRow to the Table
            tbl.Rows.Add(tr);
            string tdclass="n";
            string oldnickname = string.Empty;
            foreach (IResourceProjection resourceProjection in listofResourceProjections)
            {
                TableRow tr1 = new TableRow();
                TableCell tc2 = new TableCell();
                //var v = CastByExample( list[i - 1],o);
               // IEmployee emp = (Employee) list[i];


                if (oldnickname != resourceProjection.NickName)
                {
                    if (tdclass == "n")
                        tdclass = "n1";
                    else if (tdclass == "n1")
                        tdclass = "n";
                }

                oldnickname = resourceProjection.NickName;


                tc2.Text = resourceProjection.NickName;
                tc2.Attributes.Add("Class", tdclass);
                tr1.Cells.Add(tc2);

                //TableRow tr1 = new TableRow();
                foreach (DateTime day in EachDay(fromDate, toDate))
                {
                    TableCell tc3 = new TableCell();
                    bool b = (day.DayOfWeek == DayOfWeek.Saturday) || (day.DayOfWeek == DayOfWeek.Sunday);
                        //|| (((new BusinessPublicHoliday()).IsPublicHoliday(day)));

                    if (b)
                    {
                       // tc3.Text = "H";
                       tc3.Attributes.Add("Class", "ph");
                    }
                    else if (day >= resourceProjection.StartDate && day <= resourceProjection.EndDate)
                    {
                        tc3.Text = resourceProjection.ProjectDescription + "/" + resourceProjection.ReleaseDescription;
                        tc3.Attributes.Add("Class", tdclass);
                    }
                    else
                    {
                        tc3.Text = "";
                        tc3.Attributes.Add("Class", tdclass);
                    }
                    //TextBox txtBox = new TextBox();
                    //txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
                    // Add the control to the TableCell
                    //tc.Controls.Add(txtBox);
                    // Add the TableCell to the TableRow
                    tr1.Cells.Add(tc3);
                }
                // Add the TableRow to the Table
                tbl.Rows.Add(tr1);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            //ViewState["dynamictable"] = true;
        }

        private T CastByExample<T>(object target, T example)
        {
            return (T)target;
        }

    }

    
}